//
//  AppDelegate+Version.m
//  xk100
//
//  Created by xiaokang on 2017/6/14.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "AppDelegate+Version.h"

@implementation AppDelegate (Version)
- (void)checkAppVersion {
    NSLog(@"+++++++++++++++++++++++++++查看版本+++++++++++++++++++++++");
    [APIManager POSTRequestWith:(APIPOSTRequestState_CheckAppVersion) parameters:@{} successBlock:^(NSDictionary *successDict) {
        NSString *app_VersionNow = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        NSString *app_VersionNew = successDict[@"data"][@"version_ios"];
        NSString *app_UpdateContent = successDict[@"data"][@"ios_update"];
        NSLog(@"%@",successDict);
        NSLog(@"当前版本:%@---最新版本:%@",app_VersionNow,app_VersionNew);
        BOOL isShouldUpdate = [self compareWithVersionNow:app_VersionNow versionNew:app_VersionNew];
        
        if (isShouldUpdate) {
            [self alertToUserWithVesionNew:app_VersionNew updateContent:app_UpdateContent];
        }
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
}

- (BOOL)compareWithVersionNow:(NSString *)versionNow versionNew:(NSString *)versionNew {
    if (IsEmptyStr(versionNow) || IsEmptyStr(versionNew)) {
        return NO;
    }
    
    NSArray *versionNowArray = [versionNow componentsSeparatedByString:@"."];
    NSArray *versionNewArray = [versionNew componentsSeparatedByString:@"."];
    
    NSInteger count = MIN(versionNowArray.count, versionNewArray.count);
    
    for (NSInteger i = 0; i < count; i++) {
        NSInteger now = [[versionNowArray objectAtIndex:i] integerValue];
        NSInteger new = [[versionNewArray objectAtIndex:i] integerValue];
        if (new > now) {
            return YES;
        } else if (new < now){
            return NO;
        } else {
            continue;
        }
    }
    return NO;
}

- (void)alertToUserWithVesionNew:(NSString *)versionNew updateContent:(NSString *)updateContent {
    // 判断提示日期
    // 取出提示日期(考虑到有用户暂时忽略情况)
    NSString *tipTime = [UserDefaults objectWithKey:@"CheckAppVersionAndAlertToUserTime"];
    NSDate *alertDate = [NSDate date];
    // 如果没有取到值就直接提示用户更新
    if (!(IsEmptyStr(tipTime))) {
        alertDate = [NSDate dateWithTimeIntervalSince1970:tipTime.integerValue];
        NSLog(@"提示更新时间为:%@",[alertDate yyyy_MM_dd_HH_mm_ss_string]);
    }

    // 判断是否是今天如果是就直接提示,如果不是就不再提示
    if ([alertDate isToday]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本(%@)",versionNew] message:updateContent preferredStyle:UIAlertControllerStyleAlert];
       
        // 前往AppStore
        UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (SYSTEM_VERSION_LESS_THAN(@"10")) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1210615947"]];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1210615947"] options:@{} completionHandler:nil];
            }
        }];
        
        // 设定提示时间
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"稍后提示" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [UserDefaults setObject:[NSString calculateNumberOne:[NSString getTimestamp] calculateType:CalculateType_Add numberTwo:[NSString stringWithFormat:@"%zd",1*24*3600]] withKey:@"CheckAppVersionAndAlertToUserTime"];
        }];
        
        [alertController addAction:updateAction];
        [alertController addAction:cancelAction];
        
        [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
            NSLog(@"移除提示时间");
            [UserDefaults setObject:@"0" withKey:@"CheckAppVersionAndAlertToUserTime"];
        }];
    }
}







@end
